home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / msdos / plotting / pcgplots / gptwin.cpp < prev    next >
C/C++ Source or Header  |  1992-04-24  |  10KB  |  422 lines

  1. // GptWin.cpp
  2. // copyright 1992 Pittsburgh Supercomputing Center
  3. #include "gpt.h"
  4.  
  5.  
  6. char CGMWindow::szClass[]       = "gptCGM";
  7. char CGMWindow::szMenu[]        = "gptMenuCGM";
  8. //HMENU CGMWindow::hMenu          = NULL;
  9. HMENU CGMWindow::hWindow        = NULL;
  10. //HMENU CGMWindow::hMenuWindow    = NULL;
  11.  
  12. // ******************* CGMWindow Declarations *****************
  13. void CGMWindow::DoCaption (LPSTR mesg)
  14.       {
  15.       char szCaption [80] ;
  16.  
  17.       wsprintf (szCaption, "%s - %s", (LPSTR) mesg,
  18.                     (LPSTR) (FObj->GetFileName() )) ;
  19.  
  20.       SetWindowText (hWndThis, szCaption) ;
  21.       }
  22.  
  23.  
  24.  
  25.  
  26. // *** CGMWindow::WndProc
  27. long CGMWindow::WndProc(HWND hWnd, WORD iMessage, WORD wParam,
  28.                                                                                      LONG lParam )
  29.     {
  30.     char tmp[256];
  31.     this->iMessage = iMessage;
  32.     this->wParam   = wParam;
  33.     this->lParam   = lParam;
  34.     switch (iMessage)
  35.         {
  36.         case WM_CREATE:
  37.         hWndThis = hWnd;
  38.         SBars = new Scroller(this, DrawRect.right, DrawRect.bottom);
  39.  
  40.         DoCaption("pcGPlot");
  41.         return 0;
  42.  
  43.  
  44.  
  45.         case WM_RBUTTONDOWN:     // user initated debug break
  46.         return 0;
  47.  
  48.         case WM_INITMENUPOPUP:
  49.         if (lParam == CREATE_POP)
  50.             {
  51.             if ( CgmObj->IsValidCgm() )
  52.                 {
  53.                 EnableMenuItem (wParam, IDM_SAVE , MF_ENABLED) ;
  54.                 EnableMenuItem (wParam, IDM_META , MF_ENABLED) ;
  55.                 }
  56.             else
  57.                 {
  58.                 EnableMenuItem (wParam, IDM_SAVE , MF_GRAYED) ;
  59.                 EnableMenuItem (wParam, IDM_META , MF_GRAYED) ;
  60.                 }
  61.             }
  62.         if (lParam == CGM_MENU_POS    )
  63.             {
  64.             if ( CgmObj->NextPageExists() )
  65.                 EnableMenuItem (wParam, IDM_NEXT , MF_ENABLED) ;
  66.             else
  67.                 EnableMenuItem (wParam, IDM_NEXT , MF_GRAYED) ;
  68.             if ( CgmObj->PrevPageExists() )
  69.                 EnableMenuItem (wParam, IDM_PREVIOUS , MF_ENABLED) ;
  70.             else
  71.                 EnableMenuItem (wParam, IDM_PREVIOUS , MF_GRAYED) ;
  72.             }
  73.  
  74.  
  75.         return 0;
  76.  
  77.         case WM_PAINT:
  78.         hWindow = hWndThis;
  79.         Paint();
  80.         return 0;
  81.  
  82.         case WM_SIZE:
  83.         Size();
  84.         break;
  85.  
  86.         case WM_VSCROLL:
  87.         case WM_HSCROLL:
  88.         SBars->Scroll();
  89.         break;
  90.  
  91.         case WM_COMMAND:
  92.         switch (wParam)
  93.             {
  94.             FileObjectPt FObjSave;
  95.             case IDM_ERRORS:
  96.             if (!ErrMsg1)
  97.                 OKMsgBox("*** GPlot Errors ***", "No Errors" );
  98.             else
  99.                 OKMsgBox("*** GPlot Errors ***", "%s\n%s", ErrMsg1, ErrMsg2 );
  100.             return 0;
  101.  
  102.             case IDM_NEXT:    // Via Menu or accel key Shift-F2
  103.             if (CgmObj->NextPage())InvalidateRect(hWndThis, NULL, TRUE) ;
  104.             SBars->SetPos(0,0);
  105.             return 0;
  106.  
  107.             case IDM_PREVIOUS:  // Via Menu or accel key Shift-F1
  108.             if (CgmObj->PreviousPage()) InvalidateRect(hWndThis, NULL, TRUE);
  109.             SBars->SetPos(0,0);
  110.             return 0;
  111. /* ****
  112.             case IDM_INDEX:
  113. //            if ( !CgmObj || !CgmObj->IsValidCgm() )
  114. //                {
  115. //                OKMsgBox("*** !!! ***", "CGM Not Valid\nCannot Index" );
  116. //                return 0;
  117. //                }
  118.             IndexObjectPt IObj = new IndexObject(CgmObj);
  119.             IObj->IndexCgm(hWndThis);
  120.             WORD NewPage = IObj->GetPage();
  121.             if (NewPage != NO_PAGE_SELECTED && NewPage != CgmObj->GetPicNum() )
  122.                 {
  123.                 CgmObj->NewPage(NewPage);
  124.                 InvalidateRect(hWndThis, NULL, TRUE) ;
  125.                 }
  126.  
  127.             delete IObj;
  128.             return 0;
  129. ************ */
  130.             case IDM_INDEX:
  131.             //case IDM_PAGE_SEL:
  132.             if ( !CgmObj || !CgmObj->IsValidCgm() )
  133.                 {
  134.                 OKMsgBox("*** !!! ***", "CGM Not Valid\nCannot Index" );
  135.                 return 0;
  136.                 }
  137.             PageObjectPt PObj = new PageObject(CgmObj);
  138.             PObj->PageCgm(hWndThis);
  139.             WORD SelPage = PObj->GetPage();
  140.             if (SelPage != NO_PAGE_SELECTED && SelPage != CgmObj->GetPicNum() )
  141.                 {
  142.                 CgmObj->NewPage(SelPage);
  143.                 InvalidateRect(hWndThis, NULL, TRUE) ;
  144.                 }
  145.  
  146.             delete PObj;
  147.             return 0;
  148.  
  149.             case IDM_SAVE:           // Open file for clear text cgm output
  150.             FObjSave = new FileObject();
  151.             // Do dialog for opening CGC
  152.             strcpy(tmp, this->FObj->GetFileName());
  153.             ChangeExt( tmp, ".CGC");
  154.             FObjSave->DialogFileSetup(
  155.                                 "&Convert Picture to Clear Text CGM File :",
  156.                                 "",
  157.                                 tmp,
  158.                                 ".CGC",
  159.                                 0x4010);
  160.             if (FObjSave->OpenWrite( hWndThis))  // Clear Output File opened
  161.                 {
  162.                 HourGlass(hWnd);   // Turn on HourGlass
  163.                 CgmObj->MakeClearText(FObjSave);
  164.                 HourGlass(hWnd);
  165.                 }
  166.             delete FObjSave;
  167.             return 0;
  168.  
  169.             case IDM_META:           // Open file for metafile output
  170.             FObjSave = new FileObject();
  171.             // Do dialog for opening MET
  172.             strcpy(tmp, this->FObj->GetFileName());
  173.             ChangeExt( tmp, ".MET");
  174.             FObjSave->DialogFileSetup(
  175.                     "&Save Picture as Windows Metafile File :",
  176.                     "",
  177.                     tmp,
  178.                     ".MET", 0x4010);
  179.             if (FObjSave->OpenWrite( hWndThis))  // Metafile File opened
  180.                 {
  181.                 HourGlass(hWnd);   // Turn on HourGlass
  182.                 CgmObj->WriteMetaFile(FObjSave);
  183.                 HourGlass(hWnd);
  184.                 }
  185.             delete FObjSave;
  186.             return 0;
  187.  
  188.  
  189.             default: return 0;
  190.             }
  191.         break;
  192.  
  193.         case WM_MDIACTIVATE:
  194.         if (wParam == TRUE)    Activate();
  195.         else DeActivate();
  196.         return 0;
  197.  
  198.         case WM_QUERYENDSESSION:
  199.         case WM_CLOSE:
  200.         SendMessage (GetParent (hWnd), WM_MDIRESTORE, hWnd, 0L) ;
  201.         if (IDOK != MessageBox(hWnd,"OK to close window??","CGM Close",
  202.                                   MB_ICONQUESTION | MB_OKCANCEL)) return 0;
  203.         break;
  204.  
  205.         case WM_NCDESTROY:
  206.         delete this;
  207.         break;
  208.         }
  209.     return DefMDIChildProc( hWnd, iMessage, wParam, lParam );
  210.     }
  211.  
  212.  
  213.  
  214. void CGMWindow::Size()
  215.     {
  216.     LONG lSize = this->lParam;
  217.     WORD wType = this->wParam;
  218.     cxClient = LOWORD(lSize);
  219.     cyClient = HIWORD(lSize);
  220.     if (SBars) SBars->NewSize(cxClient, cyClient);
  221.     }
  222.  
  223.  
  224.  
  225.  
  226. void CGMWindow::Paint( void )
  227.     {
  228.     HANDLE hBrush;
  229.     HDC hdcMeta;
  230.     PAINTSTRUCT ps;
  231. /* ***************
  232.     if (hmf)
  233.         {
  234.         HDC hdc = BeginPaint(hWndThis, &ps);
  235.         //HourGlass(hWnd);
  236.         SetMapMode(ps.hdc, MM_ANISOTROPIC);
  237.         SetWindowExt(ps.hdc, DrawRect.right, DrawRect.bottom);
  238.         SetViewportExt(ps.hdc, DrawRect.right, DrawRect.bottom);
  239.         SetWindowOrg(ps.hdc,+SBars->getnHscrollPos(),+SBars->getnVscrollPos());
  240.         PlayMetaFile(ps.hdc,hmf);
  241.         EndPaint(hWndThis, &ps);
  242.         //HourGlass(hWnd);
  243.         return;
  244.         }
  245.  
  246. ************************ */
  247.  
  248.     // if new cgm
  249.     if (!CgmObj)
  250.         {
  251.         CgmObj = new cgmObject(this, FObj);
  252.         HourGlass(hWndThis);
  253.         CgmObj->GPlotInit();
  254.         HourGlass(hWndThis);
  255.         if (!CgmObj->IsValidCgm())
  256.             {
  257.             //EnableMenuItem (hMenu, IDM_PAGE_SEL , MF_GRAYED) ;
  258.             EnableMenuItem (hMenu, IDM_INDEX , MF_GRAYED) ;
  259.             }
  260.         else
  261.             {
  262.             EnableMenuItem (hMenu, IDM_INDEX , MF_ENABLED) ;
  263.             //EnableMenuItem (hMenu, IDM_PAGE_SEL , MF_ENABLED) ;
  264.             }
  265.         if (errors)
  266.              EnableMenuItem (hMenu, IDM_ERRORS , MF_ENABLED) ;
  267.         else
  268.              EnableMenuItem (hMenu, IDM_ERRORS , MF_GRAYED) ;
  269.  
  270.         DrawMenuBar(Appl::hWndFrame);
  271.         }
  272.     if ( !CgmObj->IsValidCgm() )
  273.         {
  274.         // If the DrawMenuBar goes after Begin, End Paint
  275.         //   then flickers when window moved so body mostly off screeen
  276.         BeginPaint(hWndThis, &ps);
  277.         EndPaint(hWndThis,&ps);
  278.         return;
  279.         }
  280.  
  281.     BOOL newPicture = FALSE;
  282.     if ( !CgmObj->IsSamePic())
  283.         {
  284.         HourGlass(hWndThis);
  285.         CgmObj->ZapLastPic();
  286.         CgmObj->ReadCurPic();
  287.         HourGlass(hWndThis);
  288.         //GetClientRect( hWndThis, (LPRECT) &rect );
  289.         CgmObj->SetLastCur();
  290.         char page[150];
  291.         CgmObj->GetPageText((PSTR)&page);
  292.         DoCaption(page);
  293.         newPicture = TRUE;
  294.         }
  295.     CgmObj->DisplayCurPic(hWndThis, &DrawRect, SBars);
  296.     if (!CgmObj->NextPageExists());   //will set indexed if at last  page
  297.     if (newPicture)
  298.         {
  299.         if (errors)
  300.              EnableMenuItem (hMenu, IDM_ERRORS , MF_ENABLED) ;
  301.         else
  302.              EnableMenuItem (hMenu, IDM_ERRORS , MF_GRAYED) ;
  303.  
  304.          DrawMenuBar(Appl::hWndFrame);
  305.          }
  306.  
  307.     }
  308.  
  309. // *** CGMWindow constructor
  310. // Constructor
  311. CGMWindow::CGMWindow(FileObjectPt myfile )
  312.     {
  313.     MDICREATESTRUCT mdicreate;
  314.     pPalette = NULL;              // May not need to keep around
  315.     hPal = NULL;
  316.     SBars = NULL;
  317.     errors = 0;
  318.     severe_error = FALSE;
  319.     ErrMsg1 = NULL;
  320.     ErrMsg2 = NULL;
  321.     DrawRect.top    =   DrawRect.left = 0;
  322.     DrawRect.right  =   GetSystemMetrics(SM_CXSCREEN);
  323.     DrawRect.bottom =   GetSystemMetrics(SM_CYSCREEN)
  324.                                 - GetSystemMetrics(SM_CYCAPTION)
  325.                                 - GetSystemMetrics(SM_CYMENU);
  326.  
  327.     FObj = myfile;
  328.     // FObj->DialogFileInfo( );
  329.     CgmObj = NULL;    // gplot has not "opened" yet
  330.     hmf = NULL;    // never been painted
  331.     hMenu = ::LoadMenu( Appl::hInstance,szMenu);
  332.     hMenuWindow =
  333.              GetSubMenu( hMenu,CGM_MENU_POS);
  334.  
  335.     mdicreate.szClass = szClass;
  336.     mdicreate.szTitle = "CGM" ;
  337.     mdicreate.hOwner  = Appl::hInstance ;
  338.     mdicreate.x       = CW_USEDEFAULT ;
  339.     mdicreate.y       = CW_USEDEFAULT ;
  340.     mdicreate.cx      = CW_USEDEFAULT ;
  341.     mdicreate.cy      = CW_USEDEFAULT ;
  342.     mdicreate.style   = WS_VSCROLL | WS_HSCROLL; // | WS_MAXIMIZE;
  343.     //mdicreate.lParam  = (LONG) this ;
  344.     SetObjectPt(this, (DWORD *)&mdicreate.lParam );
  345.     HWND hWndChild = SendMessage (Appl::hWndClient, WM_MDICREATE, 0,
  346.                             (LONG) (LPMDICREATESTRUCT) &mdicreate) ;
  347.     if ( ! hWndChild ) exit( FALSE );
  348.  
  349.     }
  350.  
  351. // CGMWindow Destructor
  352. CGMWindow::~CGMWindow( void )
  353.     {
  354.     BOOL hoho;
  355.     if (hmf) DeleteMetaFile(hmf);
  356.     delete SBars;
  357.     delete FObj;    // also closes open file
  358.     HourGlass(hWndThis);
  359.     delete CgmObj;
  360.     HourGlass(hWndThis);
  361.  
  362.     DeleteSz(ErrMsg1);
  363.     DeleteSz(ErrMsg2);
  364.     if (hPal) hoho = DeleteObject(hPal);
  365.     delete pPalette;
  366.  
  367.     }
  368.  
  369. void  CGMWindow::LoadMenu(HANDLE hInstance)
  370.     {
  371.     //hMenu = ::LoadMenu( hInstance,szMenu);
  372.     //hMenuWindow =
  373.     //         GetSubMenu( hMenu,CGM_MENU_POS);
  374.     }
  375.  
  376. // *** CGMWindow::Activate
  377. void CGMWindow::Activate( void )
  378.     {
  379.     SendMessage(Appl::hWndClient, WM_MDISETMENU,0,
  380.                                                           MAKELONG( hMenu, hMenuWindow));
  381.     DrawMenuBar(Appl::hWndFrame);
  382.         if (hPal)
  383.             {
  384.             HDC hDC = GetDC(hWindow);
  385.             SelectPalette(hDC, hPal,0);
  386.             RealizePalette(hDC);
  387.             //InvalidateRect(hWnd, (LPRECT)NULL, 1);
  388.             ReleaseDC(hWindow, hDC);
  389.             }
  390.     }
  391.  
  392. // *** CGMWindow::DeActivate
  393. void CGMWindow::DeActivate( void )
  394.     {
  395.     Appl::theFrame->InitMenu();
  396.     DrawMenuBar(Appl::hWndFrame);
  397.     }
  398.  
  399. // *** CGMWindow::Register - static window class register
  400. void CGMWindow::Register( void )
  401.     {
  402.     WNDCLASS wndclass;   // Structure used to register Windows class.
  403.  
  404.     wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  405.     wndclass.lpfnWndProc   = ::gptWndProc;     // the global one
  406.     wndclass.cbClsExtra    = 0;
  407.     // Reserve extra bytes for each instance of the window;
  408.     // we will use these bytes to store a pointer to the C++
  409.     // (CGMWindow) object corresponding to the window.
  410.     // the size of a 'this' pointer depends on the memory model.
  411.     wndclass.cbWndExtra    = sizeof( CGMWindow * );
  412.     wndclass.hInstance     = Appl::hInstance;
  413.     wndclass.hIcon         = LoadIcon( Appl::hInstance,"gptChild" );
  414.     wndclass.hCursor       = LoadCursor( NULL, IDC_ARROW );
  415.     wndclass.hbrBackground = GetStockObject( WHITE_BRUSH );
  416.     wndclass.lpszMenuName  = "gptMenuCGM";
  417.     wndclass.lpszClassName = szClass;
  418.  
  419.     if ( ! RegisterClass( &wndclass ) )    exit( FALSE );
  420.     }
  421.  
  422.